import 'package:flutter/material.dart';
import 'package:razer_one/app/main/constants/app_constants.dart';
import 'package:razer_one/app/main/constants/color_constants.dart';
import 'package:razer_one/app/main/extension/int_double_extension.dart';
import 'package:razer_one/app/main/model/status_model.dart';
import 'package:razer_one/app/main/view/main_btn.dart';
import 'package:razer_one/app/main/view/main_text.dart';

//网络，无数据显示页面
class MainStatusView extends StatefulWidget {
  final Widget child;
  final StatusModel statusModel; //状态模型
  final Function? emptyBtnHandle;
  final Function? netErrorHandle;
  final String emptyText;
  final String emptyBtnTitle;

  const MainStatusView({
    Key? key,
    required this.statusModel,
    required this.child,
    required this.emptyText,
    required this.emptyBtnTitle,
    this.netErrorHandle,
    this.emptyBtnHandle,
  }) : super(key: key);

  @override
  State<MainStatusView> createState() => _MainStatusViewState();
}

class _MainStatusViewState extends State<MainStatusView> {
  @override
  Widget build(BuildContext context) {
    if (widget.statusModel.isNetError) {
      return errorView;
    } else if (widget.statusModel.isShowLoadingView) {
      return const LoadingView();
    } else {
      return widget.statusModel.isShowEmpty ? emptyView : widget.child;
    }
  }

  Widget get emptyView {
    return ListView(
      children: [
        sizeH(150.px),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset("lib/app/resource/images/commond/commond_empty.png"),
            MainText(
              widget.emptyText,
              textColor: const Color(kGrayColor),
            ),
            sizeH(20),
            MainCommitBtn(widget.emptyBtnTitle, btnW: 100, handle: () {
              if (widget.emptyBtnHandle != null) widget.emptyBtnHandle!();
            }),
          ],
        )
      ],
    );
  }

  Widget get errorView {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (widget.netErrorHandle != null) widget.netErrorHandle!();
      },
      child: Container(
        alignment: Alignment.center,
        color: Colors.white,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset("lib/app/main/images/commond/commond_error.png"),
            const MainText(
              "网络不给力",
              textColor: Color(kGrayColor),
            ),
            sizeH(5),
            MainCommitBtn("重试", handle: () {
              if (widget.netErrorHandle != null) widget.netErrorHandle!();
            }, btnW: 100),
            sizeH(150.px),
          ],
        ),
      ),
    );
  }
}

class LoadingView extends StatefulWidget {
  const LoadingView({Key? key}) : super(key: key);

  @override
  _LoadingViewState createState() => _LoadingViewState();
}

class _LoadingViewState extends State<LoadingView>
    with SingleTickerProviderStateMixin {
  AnimationController? _controller;
  Animation? _colorAnim;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 1500));

    _colorAnim = Tween<double>(begin: 0, end: 1.0).animate(_controller!);
    _controller!.repeat();
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(bottom: 50),
      color: Colors.white,
      child: Center(
        child: AnimatedBuilder(
            animation: _colorAnim!,
            builder: (ctx, child) {
              double value = _colorAnim!.value;
              return ShaderMask(
                shaderCallback: (Rect bounds) {
                  return LinearGradient(
                    begin: Alignment.bottomLeft,
                    end: Alignment.topRight,
                    colors: const [
                      Color(0xFFE9E9E9),
                      Color(0xFF9B9B9B),
                      Color(0xFF9B9B9B),
                      Color(0xFFE9E9E9)
                    ],
                    stops: [value - 2, value, value, value + 0.2],
                  ).createShader(bounds);
                },
                blendMode: BlendMode.srcATop,
                child: const Text(
                  kAppTitle,
                  style: TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
                ),
              );
            }),
      ),
    );
  }
}
